import sys
import os
import io

root_d = os.getcwd()
files_avi=[]
files_mpg=[]
def render():
	for name in os.listdir(root_d):
	
		if not os.path.isfile(name):
			print name
			if os.path.exists(name + "\\render.avi"):
				print "->exists"
				file_in = name + "\\render.avi"
				file_out = name
				os.system('copy ' + file_in + " " + file_out + ".avi")
				files_avi.append(file_out)
				print file_out
	print "======================================================================================="
	for j in files_avi:
		print j
		print 'ffmpeg -y -i ' + j + ".avi -sameq " + j + ".mpg"
		
		os.system('ffmpeg -y -i ' + j + ".avi -sameq " + j + ".mpg")
		os.system('del /Q ' + j + '.avi')
		#os.system('pause')
		files_mpg.append(j + ".mpg")
		#ffmpeg -y -i tit.avi -sameq tit.mpg
	comm="copy /b " + files_mpg[0]
	for k in files_mpg[1:]:
		#copy /b 01.mpg /b +01.1.mpg /b +tit.mpg finish.mpg
		comm += " /b +" + k
	comm += " final.mpg"
	os.system(comm)
	os.system('ffmpeg -y -i final.mpg -sameq output.avi')
	os.system('del /Q final.mpg')
	for t in files_mpg:
		os.system('del /Q ' + t)
		
	if os.path.exists("snd\\snd.mp3"):
		os.system('copy snd\\snd.mp3 snd.mp3')
		os.system('ffmpeg -y -i snd.mp3 -i output.avi sounded.avi')
		os.system('del /Q output.avi snd.mp3')
		os.system('ffplay sounded.avi')
	
	return 0

	
def make_(file_name):
	if os.path.exists(file_name) :
		f = open(file_name,"r")
		a,b = os.path.split(file_name)
		
		os.chdir(a)
		root_d = os.getcwd()
		print sys.argv[0]
		os.system('copy ' + sys.argv[0] + " " + root_d + "\\" + "rend.py")
		s = f.readline()
		while s:    
			
			if not s.startswith('#'):
				
				try:
					if not os.path.exists(root_d + "\\" + s) :
						os.system('mkdir ' + root_d + "\\" + s)
				except:
					print sys.exc_info()[0]
			s = f.readline()
		f.close()
	else :
		print "ewq"
	return 0
from Tkinter import *
#import Tkinter
root=Tk()
root.title("111")
frame = Frame(root)
frame.pack()
#os.system('Pause')

def button_clicked():

	print("qwe")
	
	render()

	
import tkFileDialog
def open_dial():
	opendialog = tkFileDialog.Open()
	filename = opendialog.show()
	print filename
	make_(filename)

btn = Button(frame, text="make project dirs",command=open_dial)
btn.grid(row=0, column=1)
btn_ = Button(frame, text="assemble the project",command=button_clicked)
btn_.grid(row=1, column=1)
canvas = Canvas(width = 300, height = 200, bg = 'yellow')
canvas.pack(expand = YES, fill = BOTH)
#gif1 = PhotoImage(file = '002.gif')
#canvas.create_image(50, 10, image = gif1, anchor = NW)
root.mainloop()